package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.common.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;

import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderService;
import com.atguigu.util.DateUtils;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.net.InterfaceAddress;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;
    @Override
    public Result order(Map map) throws Exception {
        String orderData=(String)map.get("orderDate");
        Date date= DateUtils.parseString2Date(orderData);
        OrderSetting orderSetting= orderSettingDao.findByOrderDate(date);
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }else {
            int number=orderSetting.getNumber();
            int reserveration = orderSetting.getReservations();
            if (reserveration>= number){
                return new Result(false,MessageConstant.ORDER_FAIL );
            }
        }
        String telephone=(String)map.get("telephone");
        Member member=memberDao.findByTelephone(telephone);
        if (member!=null){
            Integer memberId=member.getId();
            //获取套餐
            int setmeamId =Integer.parseInt((String)map.get("setmealId"));
            Order order =new Order(memberId,date,null,null, setmeamId);
            List<Order> list=orderDao.findOrderByCondition(order);
            if (list != null&&list.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }

        }else {
            member=new Member();
            member.setName((String)map.get("name"));
            member.setSex((String)map.get("sex"));
            member.setPhoneNumber((String)map.get("telephone"));
            member.setIdCard((String)map.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        orderDao.save(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }

    @Override
    public Map findById(Integer id) throws Exception {
           Map map =orderDao.findOrderInfoById(id);
            Date orderDate=(Date)map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
            return map;

    }
}
